<?php
require_once( './core/Common/config.inc.php' );
require_once( './core/Common/common.php' );
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_USER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
//require_once( '../system/login.inc.php' );

//Class_PWD::valid_auth(ECJP_AUTH_USER,$login['staff_auth']);

$dao = new Class_M_USER;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		if( is_numeric( $_GET['id'] ) ){
			$forms  = $dao->get( $_GET['id'] );
		}
	}
	if(isset($forms)){
		if(isset($forms['birthday'])){
			$forms['birthday_year'] = gmdate('Y',strtotime($forms['birthday']));
			$forms['birthday_m'] = gmdate('m',strtotime($forms['birthday']));
		}
		if(isset($forms['zipcode'])){
			$forms['zipcode1'] = substr($forms['zipcode'],0,3);
			$forms['zipcode2'] = substr($forms['zipcode'],3,strlen($forms['zipcode'])-1);
		}
		if(isset($forms['tel_number'])){
			$forms['tel_number1'] = substr($forms['tel_number'],0,2);
			$forms['tel_number2'] = substr($forms['tel_number'],2,4);
			$forms['tel_number3'] = substr($forms['tel_number'],6,strlen($forms['tel_number'])-1);
		}
		if(isset($forms['mobile_number'])){
			$forms['mobile_number1'] = substr($forms['mobile_number'],0,2);
			$forms['mobile_number2'] = substr($forms['mobile_number'],2,4);
			$forms['mobile_number3'] = substr($forms['mobile_number'],6,strlen($forms['mobile_number'])-1);
		}
		
	}
}

if( $_SERVER['REQUEST_METHOD'] == 'GET' && $_GET['method'] == 'update'){
	    $user = $dao->get( $_GET['id'] );
	    $forms = '';
	    $err = new Class_ERROR;	    
		$chk = $dao->get_checks();
	    $forms = $_GET;
	    $forms['c_datetime']=$user['c_datetime'];
	    $forms['u_datetime']=$user['u_datetime'];
	    $forms['local_name']=$_GET['local_cd'];
	    $forms['birthday_year'] = $_GET['year4'];
	    $forms['birthday_m'] =    $_GET['moon4'];      
	    $err_mes['login_id']=$err->exist_check($forms['login_id']);
	    $err_mes['login_pwd']=$err->exist_check($forms['login_pwd']);
	    $err_mes['user_name1']=$err->exist_check($forms['user_name1']);
	    $err_mes['user_name2']=$err->exist_check($forms['user_name2']);
	    $err_mes['user_name_furikana1']=$err->exist_check($forms['user_name_furikana1']);
	    $err_mes['user_name_furikana2']=$err->exist_check($forms['user_name_furikana2']);
	    $err_mes['zipcode1']=$err->exist_check($forms['zipcode1']);
	    $err_mes['zipcode2']=$err->exist_check($forms['zipcode2']);
	    $err_mes['local_cd']=$err->exist_check($forms['local_cd']);
	    $err_mes['street_name']=$err->exist_check($forms['street_name']);
	    $err_mes['address']=$err->exist_check($forms['address']);
	    $err_mes['tel_number1']=$err->exist_check($forms['tel_number1']);
	    $err_mes['tel_number2']=$err->exist_check($forms['tel_number2']);
	    $err_mes['tel_number3']=$err->exist_check($forms['tel_number3']);
	    $err_mes['email'] =$err->exist_check($forms['email']);
	    
	    if($err->clear){
	     $err_mes['email']=$err->mail_check($forms['email']);
         $err_mes['zipcode']=$err->zipcode_check($forms['zipcode1'].'-'.$forms['zipcode2']);
         $err_mes['tel_number']=$err->tel_check($forms['tel_number1'].'-'.$forms['tel_number2'].'-'.$forms['tel_number3']);    
         $err_mes['login_id']=$err->alnum_check($forms['login_id']);
         $err_mes['login_pwd']=$err->alnum_check($forms['login_pwd']);
	    }
	   if($err->clear){
	        $values["login_id"]  =  $forms['login_id'] ;
			$values["login_pwd"] =  $forms['login_pwd'] ;
			$values["user_name1"] =  $forms['user_name1'] ;
			$values["user_name2"] =  $forms['user_name2'] ;
			$values["user_name_furikana1"] =  $forms['user_name_furikana1'] ;
			$values["user_name_furikana2"] =  $forms['user_name_furikana2'] ;
			$values["workshop_name"] =  $forms['workshop_name'] ;
			$values["workshop_name_furikana"] = $forms['workshop_name_furikana'] ;
			$values["zipcode"] =  $forms['zipcode1'].$forms['zipcode2'];
			$values["local_name"] =  $forms['local_cd'];
			$values["street_name"] =$forms['street_name'];
			$values["address"] =$forms['address'];
			$values["tel_number"] =$forms['tel_number1'].$forms['tel_number2'].$forms['tel_number3'];
			$values["mobile_number"] =$forms['mobile_number1'].$forms['mobile_number2'].$forms['mobile_number3'];
			$values["email"] =$forms['email'];
			$values["send"] =$forms['send'];
			$values["sex"] =$forms['sex'];
			$values["state"] =$forms['state'];
			$values["birthday"] =$forms['year4'].'-'.$forms['moon4'].'-01';
			$values["retreat_account"] =$forms['retreat_account'];
			$dao->edit( $forms['id'], $values );
	   }
}



$dao_local = Class_M_OTHER::get_local_array('都道府県');

$smarty->assign('year4',  Class_PWD::get_years() );
$smarty->assign('moon4',  Class_PWD::get_moons() );

if( isset($dao_local)) $smarty->assign('dao_local', $dao_local);
if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ){
	$smarty->assign('err', $err_mes);

}
$smarty->display('user_form.html');
?>